package com.brkj.communityStudy;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_TopicMember;
import com.brkj.course.model.DS_CourseClassInfo;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.util.ImgShow;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.view.BaseActivity;
import com.brkj.util.view.BaseCoreActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/20.
 */
public class TopicStudyDetailMemberActivity extends BaseActivity{
    private GridView memberGrid;
    private String topicID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.topic_study_detail_member);
        setActivityTitle("成员列表");
        setReturnBtn();

        memberGrid = (GridView) findViewById(R.id.memberGrid);

        topicID = getIntent().getStringExtra("topicID");

        getMember();
    }

    private void getMember() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetTopicStudents.params.topicID, topicID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetTopicStudents.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_TopicMember>>(){}.getType();
                List<DS_TopicMember> memberList  = gson.fromJson((String)o, listType);

                CourseClassSecondAdapter adapter =
                        new CourseClassSecondAdapter(TopicStudyDetailMemberActivity.this, memberList);

                memberGrid.setAdapter(adapter);
            }
        });
    }



    public class CourseClassSecondAdapter extends BaseAdapter {
        private ListItemView listItemView;
        protected LayoutInflater listContainer;
        private Context context;
        private List<DS_TopicMember> memberList;

        public CourseClassSecondAdapter(Context context, List<DS_TopicMember> memberList){
            this.context = context;
            this.memberList = memberList;

            listContainer = LayoutInflater.from(context);
        }

        private  class ListItemView{
            public TextView name;
            public ImageView headImg;
        };

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return memberList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 自定义视图
            listItemView = new ListItemView();
            // 获取list_item布局文件的视图
            if (convertView == null) {
                convertView = listContainer.inflate(R.layout.topic_study_member_item, null);
                // 获取控件对象
                listItemView.name = (TextView) convertView.findViewById(R.id.name);
                listItemView.headImg = (ImageView) convertView.findViewById(R.id.headImg);

                // 设置控件集到convertView
                convertView.setTag(listItemView);
            } else {
                listItemView = (ListItemView) convertView.getTag();
            }

            listItemView.name.setText(memberList.get(position).getName());

            ImgShow.display(listItemView.headImg, memberList.get(position).getUserHeadImg());

            return convertView;
        }

    }

    
}
